package com.example.aop.proxy;

import com.example.aop.invocation.CGLIBInterceptor;
import com.example.aop.invocation.JDKinvocaion;
import net.sf.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Callback;
//import org.springframework.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import static org.springframework.cglib.proxy.Enhancer.create;

public class main {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //静态代理
//        proxyHouse proxyHouse=new proxyHouse(new ResultHouse());
//        proxyHouse.rentHouse();
//        proxyHouse.outHouse();

        //动态代理JDK
//         HouseSubject target=new ResultHouse();
//         HouseSubject instance = (HouseSubject) Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class[]{HouseSubject.class},
//                new JDKinvocaion(target));
//         instance.outHouse();
//         instance.rentHouse();
        HouseSubject target=new ResultHouse();
        HouseSubject proxy = (HouseSubject) Enhancer.create(HouseSubject.class,new CGLIBInterceptor(target));
        proxy.rentHouse();
        proxy.outHouse();

        Class<?> aClass = Class.forName("com.example.aop.Clas");
        Object o = aClass.getDeclaredConstructor().newInstance();
        System.out.println(o);

        Method print = aClass.getDeclaredMethod("print", Integer.class);
        print.setAccessible(true);
        print.invoke(o,5);
    }
}
